<style>
    * {
        margin: 0;
        padding: 0;
    }

    video {
        max-width: 100%;
    }

    h1, div, video {
        margin: 5px 20px;
    }
</style>

<title>Calculate Recording Duration | RecordRTC</title>
<h1>Calculate Recording Duration using RecordRTC</h1>

<br>

<div>
    <button id="btn-start-recording">Start Recording</button>
    <button id="btn-stop-recording" disabled>Stop Recording</button>
</div>

<hr>
<video controls autoplay playsinline></video>

<script src="/RecordRTC.js"></script>
<script>
var video = document.querySelector('video');
var dateStarted;

function captureCamera(callback) {
    navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then(function(camera) {
        callback(camera);
    }).catch(function(error) {
        alert('Unable to capture your camera. Please check console logs.');
        console.error(error);
    });
}

function stopRecordingCallback() {
    video.srcObject = null;
    
    var blob = recorder.getBlob();
    video.src = URL.createObjectURL(blob);

    recorder.camera.stop();
    recorder = null;
}

function calculateTimeDuration(secs) {
    var hr = Math.floor(secs / 3600);
    var min = Math.floor((secs - (hr * 3600)) / 60);
    var sec = Math.floor(secs - (hr * 3600) - (min * 60));

    if (min < 10) {
        min = "0" + min;
    }

    if (sec < 10) {
        sec = "0" + sec;
    }

    if(hr <= 0) {
        return min + ':' + sec;
    }

    return hr + ':' + min + ':' + sec;
}

var recorder; // globally accessible

document.getElementById('btn-start-recording').onclick = function() {
    this.disabled = true;
    captureCamera(function(camera) {
        video.srcObject = camera;

        recorder = RecordRTC(camera, {
            recorderType: MediaStreamRecorder,
            mimeType: 'video/webm',
            timeSlice: 1000 // pass this parameter
        });

        recorder.startRecording();
        dateStarted = new Date().getTime();

        (function looper() {
            if(!recorder) {
                return;
            }

            document.querySelector('h1').innerHTML = 'Recording Duration: ' + calculateTimeDuration((new Date().getTime() - dateStarted) / 1000);

            setTimeout(looper, 1000);
        })();

        // release camera on stopRecording
        recorder.camera = camera;

        document.getElementById('btn-stop-recording').disabled = false;
    });
};

document.getElementById('btn-stop-recording').onclick = function() {
    this.disabled = true;
    recorder.stopRecording(stopRecordingCallback);
};
</script>

<footer style="margin-top: 20px;"><small id="send-message"></small></footer>
<script src="https://www.webrtc-experiment.com/common.js"></script>
